package br.edu.unifei.mestrado.view;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections15.Transformer;

import br.edu.unifei.mestrado.mem.graph.Node;
import br.edu.unifei.mestrado.mem.graph.Relationship;
import edu.uci.ics.jung.graph.Graph;

public class PartitionClusterer implements Transformer<Graph<Node, Relationship>, Set<Set<Node>>> {

	private int qtdPartitions;

	public PartitionClusterer(int qtdPartitions) {
		this.qtdPartitions = qtdPartitions;
	}

	//Separa o grafo em conjuntos de arestas de acordo com a parti��o
	@Override
	public Set<Set<Node>> transform(Graph<Node, Relationship> graph) {

		Map<Integer, Set<Node>> hash = new HashMap<Integer, Set<Node>>();

		Set<Set<Node>> result = new HashSet<Set<Node>>();

		for (Node node : graph.getVertices()) {
			Set<Node> set = hash.get(node.getPartition());
			if(set == null) {
				set = new HashSet<Node>();
				hash.put(node.getPartition(), set);
				result.add(set);
			}
			set.add(node);
		}
		return result;
	}

	public int getQtdPartitions() {
		return qtdPartitions;
	}
}
